using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using EntLibEC.Cmn;
using EntLibEC.Biz;

using EntLibEC.Biz.Report;

namespace EntLibEC.View.Report
{
	/// <summary>
	/// Summary description for POPayReportByVendor.
	/// </summary>
	public partial class POPayReportByVendor : PageBase
	{

		protected EntLibEC.View.Cmn.Currency ucCurrency;
		protected EntLibEC.View.Cmn.POStatus ucPOStatus;
		protected EntLibEC.View.Cmn.PM ucPPM;
		protected EntLibEC.View.Cmn.POPayStatus ucPOPayStatus;
		protected EntLibEC.View.Cmn.YNStatus uclIsConsignment;

		decimal totalPOAmtSum, totalAlreadyPayAmtSum, totalUnPayAmtSum;
	
		protected void Page_Load(object sender, System.EventArgs e)
		{
			base.CheckAccess((int)AppEnum.Privilege.FinanceReportSearch,true ,null);

			txtDateFromInStock.Attributes["onclick"]= "javascript:calendar()";
			txtDateToInStock.Attributes["onclick"]= "javascript:calendar()";

			if ( !Page.IsPostBack)
			{
				ucCurrency.BindCurrency(true);
				ucPOStatus.BindStatus(true);
				ucPPM.BindPM(true);
				ucPOPayStatus.BindStatus(true);
				txtDateFromInStock.Text = DateTime.Now.AddMonths(-1).ToString(AppConst.DateFormat);
				txtDateToInStock.Text = DateTime.Now.ToString(AppConst.DateFormat);

				uclIsConsignment.BindStatus(true);	//added by entlib 
			}
		}

		private bool CheckForm()
		{
			return true;
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);

		}
		#endregion

		protected void btnSearch_Click(object sender, System.EventArgs e)
		{
			BindGrid();
		}

		private void BindGrid()
		{
			if ( !CheckForm() )
				return;
			Hashtable ht = new Hashtable(7);

			if ( ucPOStatus.StatusSysNo != AppConst.IntNull)
				ht.Add("POStatus", ucPOStatus.StatusSysNo);
			if ( ucCurrency.CurrencySysNo != AppConst.IntNull)
				ht.Add("CurrencySysNo", ucCurrency.CurrencySysNo);
			if ( ucPPM.PMSysNo != AppConst.IntNull )
				ht.Add("CreateUserSysNo", ucPPM.PMSysNo);
			if ( ucPOPayStatus.StatusSysNo != AppConst.IntNull )
				ht.Add("PayStatus", ucPOPayStatus.StatusSysNo);

			//added by entlib 2006/05/23
			if (uclIsConsignment.YNSysNo != AppConst.IntNull)
			{
				ht.Add("IsConsign", uclIsConsignment.YNSysNo);
			}

			if ( txtDateFromInStock.Text.Trim() != "" )
				ht.Add("DateFromInStock", txtDateFromInStock.Text.Trim() );
			if ( txtDateToInStock.Text.Trim() != "" )
				ht.Add("DateToInStock", txtDateToInStock.Text.Trim());

			DataSet ds = ReportManager.GetInstance().GetPOPayReportByVendorDs(ht);

			DataGrid1.DataSource = ConvertDs(ds);
			DataGrid1.DataBind();

			int rows = 0;
			if ( Util.HasMoreRow(ds) )
			{
				rows = ds.Tables[0].Rows.Count;
				btnExport.Enabled = true;
			}
			Assert(lblMessage, rows.ToString() + " record(s)", 1);
		}

		private DataSet ConvertDs(DataSet ds)
		{
			if ( !Util.HasMoreRow(ds))
				return ds;

			ds.Tables[0].Columns.Add("VendorCombo");
			ds.Tables[0].Columns.Add("TotalPOAmtCvt");
			ds.Tables[0].Columns.Add("TotalAlreadyPayAmtCvt");
			ds.Tables[0].Columns.Add("TotalUnPayAmtCvt");

			foreach( DataRow dr in ds.Tables[0].Rows )
			{
				decimal totalPOAmt, totalAlreadyPayAmt, totalUnPayAmt;

				totalPOAmt = Util.TrimDecimalNull(dr["totalPOAmt"]);
				totalAlreadyPayAmt = Util.TrimDecimalNull(dr["totalAlreadyPayAmt"]);
				totalUnPayAmt = totalPOAmt - totalAlreadyPayAmt;

				totalPOAmtSum += totalPOAmt;
				totalAlreadyPayAmtSum += totalAlreadyPayAmt;
				totalUnPayAmtSum += totalUnPayAmt;

				dr["TotalPOAmtCvt"] = totalPOAmt.ToString(AppConst.DecimalFormatWithGroup);
				dr["TotalAlreadyPayAmtCvt"] = totalAlreadyPayAmt.ToString(AppConst.DecimalFormatWithGroup);
				dr["TotalUnPayAmtCvt"] = totalUnPayAmt.ToString(AppConst.DecimalFormatWithGroup);

				string vendorname = Util.TrimNull(dr["VendorName"]);
				if ( vendorname.Length>10)
					dr["VendorCombo"] = "<div title='" + vendorname + "'>" + vendorname.Substring(0,10) + "...</div>";
				else
					dr["VendorCombo"] = "<div title='" + vendorname + "'>" + vendorname + "...</div>";
			}
			return ds;
		}

		private string ToFooterString(int x)
		{
			if ( x == 0)
				return "";
			return x.ToString(AppConst.IntFormatWithGroup);
		}
		private string ToFooterString(decimal x)
		{
			if ( x == 0)
				return "";
			return x.ToString(AppConst.DecimalFormatWithGroup);
		}

		protected void btnExport_Click(object sender, System.EventArgs e)
		{
			Util.ToExcel(DataGrid1, "", "");
		}

		private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
		{
			if(e.Item.ItemType == ListItemType.Footer)
			{
				e.Item.Cells[1].Text = ToFooterString(totalPOAmtSum);
				e.Item.Cells[2].Text = ToFooterString(totalAlreadyPayAmtSum);
				e.Item.Cells[3].Text = ToFooterString(totalUnPayAmtSum);
			}
		}
	}
}
